#include "connection.hpp"

void cb(mmq::Channel::ptr &channel, const std::string &consumer_tag, const mmq::BasicProperties *bp, const std::string &body)
{
    std::cout << consumer_tag << "消费了消息：" << body << std::endl;
    channel->BasicAck(bp->id());
}

int main(int argc, char *argv[])
{
    if(argc != 2)
    {
        std::cout << "usage: ./consume_client queue1\n";
        return -1;
    }
    // 1. 实例化异步工作线程对象
    mmq::AsyncWorker::ptr worker = std::make_shared<mmq::AsyncWorker>();
    // 2. 实例化连接对象
    mmq::Connection::ptr conn = std::make_shared<mmq::Connection>("127.0.0.1", 8085, worker);
    // 3. 通过连接创建信道
    mmq::Channel::ptr channel = conn->OpenChannel();
    // 4. 根据信道提供的服务完成所需
    // 4.1 声明一个交换机，类型为广播模式
    google::protobuf::Map<std::string, std::string> tmp_map;
    channel->DeclareExchange("exchange1", mmq::ExchangeType::TOPIC, true, false, tmp_map);
    // 4.2 声明一个队列queue1
    channel->DeclareQueue("queue1", true, false, false, tmp_map);
    // 4.3 声明一个队列queue2
    channel->DeclareQueue("queue2", true, false, false, tmp_map);
    // 4.4 绑定queue1-exchange1，binding_key设置为queue1
    channel->QueueBind("exchange1", "queue1", "queue1");
    // 4.5 绑定queue2-exchange1，binding_key设置为news.music.#
    channel->QueueBind("exchange1", "queue2", "news.music.#");
    auto func = std::bind(cb, channel, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
    channel->BasicConsume(argv[1], "consumer1", false, func);
    
    while(1) std::this_thread::sleep_for(std::chrono::seconds(3));

    conn->CloseChannel(channel);
    return 0;
}